package uit.jee.lib.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.jee.lib.bo.BookBO;
import uit.jee.lib.dto.BookDTO;

/**
 *
 * @author ngloc_it
 */
@WebServlet(name="BookSameType", urlPatterns={"/BookSameType"})
public class BookSameType extends HttpServlet {
    private int itemsPerPage = 50;
    private boolean isDataEnd = false;
   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();

        String function = req.getParameter("function");
        String typeBookId = req.getParameter("booktypeid");
        String typeBookName = req.getParameter("booktypename");
        String strCurrentPage = req.getParameter("page");

        if(typeBookId == null)
            typeBookId = "";

        if(function == null)
            function = "load";

        int currentPage = 0;
        if (strCurrentPage != null) {
            currentPage = Integer.parseInt(strCurrentPage);
        }

        if(function.equalsIgnoreCase("load")){
            try {
                ArrayList<BookDTO> books = LoadBookSame(typeBookId);
                if((books != null) &&
                        (!books.isEmpty())){
                    session.setAttribute("books", books);
                }
            } catch (Exception ex) {
                Logger.getLogger(BookSameType.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(function.equalsIgnoreCase("reload")){
            try {
                ReloadBookSame(currentPage, typeBookId);
            } catch (Exception ex) {
                Logger.getLogger(BookSameType.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        session.setAttribute("page", currentPage);


//         Control to the page show the books with the same type
//         by type id

        String path = "./jsps/BookGroup.jsp";
        resp.sendRedirect(path);
    }

    private ArrayList<BookDTO> LoadBookSame(String typeId) throws Exception{
        BookBO bookBo = new BookBO();
        ArrayList<BookDTO> books = bookBo.getBookByBookTypeId(typeId);
        
        if (books != null) {            
            for (int i = itemsPerPage; i < books.size(); i++) {
                books.remove(i);
                i--;
            }
        }
        return books;
    }

    /**
     * Nguoi ngo da xa xua, nhung nguoi bong lai ve,
     * @param currentPage
     */
    private void ReloadBookSame(int currentPage, String type) throws Exception{
        BookBO bookBo = new BookBO();
        ArrayList<BookDTO> books = bookBo.getBookByBookTypeId(type);

        int nextStep = currentPage * itemsPerPage;
        int numBook = books.size();

        if(nextStep >= numBook)
            return;

        int i;
        for(i = nextStep; i < numBook; i++){
            BookDTO book = books.remove(0);

            //resp data for ajax
        }
    }
}